﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;
using PPAP.Model;

namespace PPAP.Web.Master
{
    public partial class UsersEdit : PageBase
    {
        UsersBO bo = new UsersBO();
        DepartmentBO dbo = new DepartmentBO();
        LogBO lbo = new LogBO();
        TestDepartmentBO sbo = new TestDepartmentBO();
        TestDepartmentCardBO tdbo = new TestDepartmentCardBO();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                txtEmail.Attributes.Add("onblur", "emailtouser()");
                DataSet dsuser = bo.GetList();
                ddlUser.Items.Add(new ListItem("No", "No"));
                foreach (DataRow dr in dsuser.Tables[0].Rows)
                {
                    ddlUser.Items.Add(new ListItem(dr["username"].ToString(), dr["username"].ToString()));
                }

                DataSet dsuser1 = bo.GetListForCountry("China");
                ddlUser1.Items.Add(new ListItem("No", "No"));
                foreach (DataRow dr in dsuser1.Tables[0].Rows)
                {
                    ddlUser1.Items.Add(new ListItem(dr["username"].ToString(), dr["username"].ToString()));
                }

                DataSet dsuser2 = bo.GetListForCountry("Germany");
                ddlUser2.Items.Add(new ListItem("No", "No"));
                foreach (DataRow dr in dsuser2.Tables[0].Rows)
                {
                    ddlUser2.Items.Add(new ListItem(dr["username"].ToString(), dr["username"].ToString()));
                }

                DataSet sds = sbo.GetList("");
                foreach (DataRow dr in sds.Tables[0].Rows)
                {
                    DropDownList1.Items.Add(new ListItem(dr["TESTITEMNAME"].ToString(), dr["ID"].ToString()));
                    cbDe.Items.Add(new ListItem(dr["TESTITEMNAME"].ToString(), dr["id"].ToString()));
                }
                DataSet dlist = dbo.GetList("");
                foreach (DataRow dr in dlist.Tables[0].Rows)
                {
                    ddlDepartment.Items.Add(new ListItem(dr["DEPARTMENT"].ToString(), dr["ID"].ToString()));
                }
                DataSet plist = dbo.GetRoles();
                foreach (DataRow dr in plist.Tables[0].Rows)
                {
                    ddlPosition.Items.Add(new ListItem(dr["rolename"].ToString(), dr["rolename"].ToString()));
                }
                if (Request.QueryString["DID"] != null)
                {
                    LoadDep(Request.QueryString["DID"].ToString());
                }
                rbtY_CheckedChanged(null, null);
                ddlPosition_SelectedIndexChanged(null, null);

                //LoadData();
            }
            if (Request.QueryString["deid"] != null)
            {
                dele(Request.QueryString["deid"].ToString());
            }
        }



        //private void LoadData()
        //{
        //    DataSet ds = bo.GetList("");
        //    GridView1.DataSource = ds;
        //    GridView1.DataBind();
        //}

        private void dele(string id)
        {
            int num = bo.Delete(int.Parse(id));
            lbo.WriteLog(CurrentUser.Id, "Delete User");
            if (num > 0)
                AlertMessage("Deleted successful");
            //LoadData();
        }

        private void LoadDep(string id)
        {
            Model.Users d = bo.GetModel(int.Parse(id));
            txtEmail.Text = d.EMAIL;
            txtFamilyname.Text = d.FAMILYNAME;
            txtFirstname.Text = d.FIRSTNAME;
            txtMobilephone.Text = d.MOBILEPHONE;
            txtUserName.Text = d.USERNAME;
            ddlDepartment.SelectedValue = d.DEPARTMENTID.ToString().Trim();
            ddlPosition.SelectedValue = d.POSITION.ToString().Trim();
            rbtN.Checked = d.ISEXECUTEIONSPECTIONTASK == 0 ? true : false;
            rbtY.Checked = d.ISEXECUTEIONSPECTIONTASK == 1 ? true : false;
            txtUserName.Enabled = false;
            txtEmail.Enabled = false;
            DropDownList1.SelectedValue = d.TESTDEPARTMENT.ToString();
            ddlUser.SelectedValue = d.Superior;
            ddlUser1.SelectedValue = d.Superior1;
            ddlUser2.SelectedValue = d.Superior2;
            ddlCountry.SelectedValue = d.Country;
            txtSort.Text = d.Sort;
            txtFax.Text = d.Fax;

            DataSet dscard = tdbo.GetList(" username='"+d.USERNAME+"' ");
            foreach (DataRow dr in dscard.Tables[0].Rows)
            {
                foreach (ListItem item in cbDe.Items)
                {
                    ListItem cb = item as ListItem;
                    if (cb != null&&dr["TESTDEPARTMENT"].ToString()==item.Value)
                    {
                        cb.Selected = true;
                    }
                }
            }
            
            Button1.Text = "Save";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Model.Users d = new Model.Users();
            int num = 0;
            if (txtFirstname.Text.Trim() == "" || txtFamilyname.Text.Trim() == "" || txtUserName.Text.Trim() == "")
            {
                AlertMessage("The text frame not is empty");
                return;
            }
            if (Button1.Text == "Save")
                d = bo.GetModel(int.Parse(Request.QueryString["DID"].ToString()));
            d.DEPARTMENTID = int.Parse(ddlDepartment.SelectedValue);
            d.EMAIL = CheckWord(txtEmail.Text.Trim().ToLower());
            d.FAMILYNAME = CheckWord(txtFamilyname.Text);
            d.FIRSTNAME = CheckWord(txtFirstname.Text);
            d.ISEXECUTEIONSPECTIONTASK = rbtY.Checked ? 1 : 0;
            d.MOBILEPHONE = CheckWord(txtMobilephone.Text);
            d.POSITION = ddlPosition.SelectedValue;
            d.USERNAME = CheckWord(txtUserName.Text.Trim().ToLower());
            d.TESTDEPARTMENT = int.Parse(DropDownList1.SelectedValue);
            d.Superior = ddlUser.SelectedValue;
            d.Superior1 = ddlUser1.SelectedValue;
            d.Superior2 = ddlUser2.SelectedValue;
            d.Country = ddlCountry.SelectedValue;
            d.Sort = CheckWord(txtSort.Text);
            d.Fax = CheckWord(txtFax.Text);
            if (Button1.Text == "Add")
            {
                if (bo.GetList(" username='" + d.USERNAME + "'").Tables[0].Rows.Count > 0)
                {
                    AlertMessage("User name already exists");
                    return;
                }
                if (bo.CheckEamil(d.USERNAME))
                {
                    AlertMessage("User name already exists");
                    return;
                }
                if (bo.CheckEamil(d.EMAIL))
                {
                    AlertMessage("Email already exists");
                    return;
                }
                d.PASSWORD = "123456";
                num = bo.Add(d);
                lbo.WriteLog(CurrentUser.Id, "Add User:" + d.USERNAME);
            }
            else
            {
                d.ID = int.Parse(Request.QueryString["DID"].ToString());
                num = bo.Update(d);
                lbo.WriteLog(CurrentUser.Id, "Edit User:" + d.USERNAME);
            }
            if (rbtY.Checked)
            {
                tdbo.Delete(d.USERNAME);
                foreach (ListItem item in cbDe.Items)
                {

                    ListItem cb = item as ListItem;
                    
                    if (cb != null)
                    {
                        if (cb.Selected)
                        {
                            TestDepartmentCard card = new TestDepartmentCard();
                            card.TESTDEPARTMENT = int.Parse(cb.Value);
                            card.USERNAME = d.USERNAME;
                            tdbo.Add(card);
                        }
                    }

                }
            }
            if (num > 0)
            {
                txtUserName.Text = "";
                txtMobilephone.Text = "";
                txtFirstname.Text = "";
                txtFamilyname.Text = "";
                txtEmail.Text = "";
                txtUserName.Enabled = true;
                Button1.Text = "Add";
                //AlertMessage("Save success");
                AlertReflashOpener("Save success");
            }
            //LoadData();
        }

        protected void rbtY_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtY.Checked)
            {
                trde.Visible = true;

            }
            else
            {
                trde.Visible = false;
            }
        }

        protected void txtEmail_TextChanged(object sender, EventArgs e)
        {
            txtUserName.Text = txtEmail.Text;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlPosition.SelectedValue == "BDNT/D Engineer")
            {
                truser.Visible = true;
                lbSuperior.Visible = false;
                ddlUser.Visible = false;
            }
            else
            {
                truser.Visible = false;
                lbSuperior.Visible = true;
                ddlUser.Visible = true;
            }
        }

    }
}